package org.liuqm.servlet;

import com.alibaba.fastjson.JSON;
import org.liuqm.pojo.SysMenu;
import org.liuqm.service.ISysMenuService;
import org.liuqm.service.ISysUserService;
import org.liuqm.service.impl.SysMenuServiceImpl;
import org.liuqm.service.impl.SysUserServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "MenuServlet" , urlPatterns = "/system/menu")
public class MenuServlet extends HttpServlet {


    private ISysMenuService sysMenuService;

    @Override
    public void init(ServletConfig config) throws ServletException {
        sysMenuService = new SysMenuServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、创建out对象
        PrintWriter out = resp.getWriter();
        List<SysMenu> menus = sysMenuService.selectMenuTreeByUserId(1l);
        out.print(JSON.toJSONString(sysMenuService.buildMenus(menus)));
        out.flush();
        out.close();
    }
}
